package com.gitee.llj.shiro.controller;

import com.gitee.llj.entity.sys.SysUser;
import com.gitee.llj.shiro.service.ISysUserService;
import com.gitee.llj.tools.base.ResultModel;
import com.gitee.llj.tools.base.ResultStatus;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>用户controller</p>
 *
 * @Author llj
 * @Date 2018/3/29
 */
@RestController
@RequestMapping("/api/user")
public class SysUserController extends BaseController{

    @Resource
    private ISysUserService sysUserService;

    @RequestMapping("/get-by-username")
    @RequiresPermissions(value="user:group")
    public ResultModel getUserInfoByUserName(@RequestParam("username") String username){
        if (StringUtils.isBlank(username)){return new ResultModel(ResultStatus.FAIL.getSuccess(),"用户名不能为空");}
        return sysUserService.getUserInfoByUserName(username);
    }

    /**
     * 获得当前用户信息
     * @return
     */
    @RequestMapping("/current-user")
    public ResultModel getCurrentUserInfo(){
        SysUser currentUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
        return ResultModel.ok(currentUser);
    }
}
